<template>
  <div style="padding: 3rem;">
    <div style="display: flex; justify-content: space-between; align-items: center;">
      <el-button type="primary" @click="dialogVisible = true">新建</el-button>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="规格名称">
          <el-input v-model="searchForm.specName" placeholder="请输入规格名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog v-model="dialogVisible" title="新建规格" width="500">
      <el-form :model="newSpecForm">
        <el-form-item label="规格名称">
          <el-input v-model="newSpecForm.specName" placeholder="请输入规格名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleAddSpecification">确定</el-button>
      </div>
    </el-dialog>
    <el-table :data="tableData" style="width: 100%; margin-top: 10px;">
      <el-table-column prop="id" label="规格ID" width="" />
      <el-table-column prop="specName" label="规格名称" width="" />
      <el-table-column fixed="right" label="操作" min-width="120">
      <template #default>
        <el-button link type="primary" size="small">编辑</el-button>
      </template>
    </el-table-column>
    </el-table>
    <div class="demo-pagination-block" style="margin-top: 20px;">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
        :page-sizes="[5, 10, 20, 40]" :size="size" :disabled="disabled" :background="background"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script lang="ts" setup>
import { searchSpecifications, addSpecification } from "@/api/specification";
import { ref, onMounted } from 'vue'
import type { ComponentSize } from 'element-plus'

const currentPage = ref(1)
const pageSize = ref(10)
const size = ref<ComponentSize>('default')
const background = ref(false)
const disabled = ref(false)
const tableData = ref([])
const total = ref(0)
const searchForm = ref({
  specName: ''
})
const dialogVisible = ref(false)
const newSpecForm = ref({
  specName: ''
})

const fetchData = async () => {
  const response = await searchSpecifications(currentPage.value, pageSize.value, searchForm.value)
  tableData.value = response.rows
  total.value = response.total
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  fetchData()
}

const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

const handleAddSpecification = async () => {
  await addSpecification(newSpecForm.value)
  dialogVisible.value = false
  fetchData()
}

onMounted(() => {
  fetchData()
})
</script>
